<?php

require('config.php');
$language_link=$_GET['id'];

$page = new rootLayoutPage();

/*$page->importJavaScriptTop('http://maps.google.com/maps/api/js?sensor=false');
$page->addJavaScript('uiMap/jquery.fn.gmap.js');
$page->addJavaScript('uiMap/jquery.ui.map.services.js');*/

$page->setBaliseTitle('place_location_balise');
$page->setDescription('place_location_description');
//-- Menu Page ----------------------------------------------//
if(!$_SESSION['id_user']) redirect('connexion');

if($language_link!=null && $language_link>0){
	if(onlyNumber($language_link)){
		$location = modelLocationMap::getList($language_link);
		if(!$location) redirectFront(LANGUAGE_FOLDER."/index");
		if(($location->id_createur != $_SESSION['id_user']) and (!modelUser::isUserGroupAdmin($_SESSION['id_user'])))redirectFront(LANGUAGE_FOLDER."/index");
	}else
		redirectFront(LANGUAGE_FOLDER."/index");
}else
	redirectFront(LANGUAGE_FOLDER."/index");

$xml = tmxTranslate('place-marker.xml', REP_XLAT_TEMPLATE);
$menu = new layoutMenuLeft();
	$menuPage = new layoutMenuPage();
	$menuPage->setTable('category_map');
	//$menu->setIdShow($category_map->id_menu_left_tree);
	$menu->pushMenu($menuPage);

//--------Menu indicator--------------//
$menuIndictor = new layoutMenuIndicator();
	$page->setMenuIndicator($menuIndictor);

//************************//
		$html_name = $location->url_rewriting.'-'.$location->language_link.'.html';
		if(isset($_POST['create'])) {
			 try {
				//if(!isTranslating()){
				  	$latitude = $_POST['latitude'];
	      		  	$longitude = $_POST['longitude'];
	      		  	$getPlace = modelLocationMap::getPlaceById($language_link);
					$id_map_address_shared = $getPlace->id_map_address_shared;
					modelLocationMap::updateLatitude($id_map_address_shared,$latitude,$longitude);
					Persistence::register(PERSIST_MESSAGE, new Message($xml['message_1'], Message::INFO));
				//}else{
				//	Persistence::register(PERSIST_MESSAGE, new Message($xml['message_2'], Message::INFO));
				//}
			 }
		    catch(Exception $e) {
		        Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message::AVERT));
		    }

			redirectFront('upload-photo-video','id='.$_GET['id']);
	    }
	    if(isset($_POST['modify'])) {
			 try {
				  	$latitude = $_POST['latitude'];
	      		  	$longitude = $_POST['longitude'];
	      		  	$getPlace = modelLocationMap::getPlaceById($language_link);
					$id_map_address_shared = $getPlace->id_map_address_shared;
					modelLocationMap::updateLatitude($id_map_address_shared,$latitude,$longitude);
					Persistence::register(PERSIST_MESSAGE, new Message($xml['message_1'], Message::INFO));
			 }
		    catch(Exception $e) {
		        Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message::AVERT));
		    }
		    if(!isset($_SESSION['id_property'])){
				redirectFrontHtml($html_name);
		    }else{
		    	redirectFront('upload-photo-video','id='.$_GET['id']);
		    }
	    }
	    if(isset($_POST['skip'])) {
	    	redirectFront('upload-photo-video','id='.$_GET['id']);
	    }


$tpl_file = 'place-marker.tpl';
$place_location = new flyLayout(REP_TPL . $tpl_file);
$place_location->start();
if(DISPLAY_MAP_FRONT==1){
	$page->importJavaScriptTop('http://maps.google.com/maps/api/js?sensor=true');
	$page->addJavaScript('uiMap/jquery-ui.min.js');
	$page->addJavaScript('uiMap/jquery.fn.gmap.js');
	$page->addJavaScript('uiMap/jquery.ui.map.services.js');
	$page->importStyleSheet('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/ui-lightness/jquery-ui.css');
}

$map = modelProvince::getProvince($location->id_province);
	if($map and $map->latitude > 0 and $map->longitude > 0 and $map->zome_level > 0){
    	//$place_location->setVariable('latitude_map', $map->latitude);
		//$place_location->setVariable('longitude_map', $map->longitude);
		$place_location->setVariable('latitude_map', $map->latitude!=''?$map->latitude:DEFAUFT_LATITUDE);
		$place_location->setVariable('longitude_map',$map->longitude!=''?$map->longitude:DEFAUFT_LONGITUDE);
		$place_location->setVariable('zome_level', $map->zome_level);
		
    }else{
    	$place_location->setVariable('latitude_map', DEFAUFT_LATITUDE);
		$place_location->setVariable('longitude_map', DEFAUFT_LONGITUDE);
		$place_location->setVariable('zome_level', DEFAUFT_ZOME_LEVEL_DETAIL);
    }
    $place_location->showBlock('show_point');
if(isset($_GET['modify']) or isset($_SESSION['id_property'])){
	if($location->latitude > 0 and $location->longitude > 0){
		//$place_location->setVariable('place_name',addslashes($location->name_place));
		$place_location->setVariable('address', addslashes($location->address));
		$province = modelProvince::getProvince($location->id_province);
		//$place_location->setVariable('province',addslashes($province->title));
		//$city = modelCity::getCity($location->id_city);
		//$place_location->setVariable('city',addslashes($city->title));
		/*$district = modelDistrict::getDistrict($location->id_district);
		if($district){
			$place_location->setVariable('district',addslashes($district->title));
		}*/
		//$place_location->setVariable('zip_code', addslashes($location->zip_code));

		/*if(isset($_SESSION['id_user']) and $location->id_createur == $_SESSION['id_user']){
			$photo_map = modelPhoto::getMainPhotoLocation($location->language_link);
		}else{
			$photo_map = modelPhoto::getMainPhotoLocation_out($location->language_link);
		}
		$format_map = modelUploadFormat::getObject(FORMAT_PREVIEW_MAP);
		if($photo_map){
			$fichier_map = modelUploadFile::getObject($photo_map->id_fichier);
			if(file_exists($fichier_map->url_repertoire.$format_map->url_format.$fichier_map->nom_serveur)){
				list($width, $height, $type, $attr) = getimagesize($fichier_map->url_repertoire.$format_map->url_format.$fichier_map->nom_serveur);
				$url_image_map = $fichier_map->url_repertoire.$format_map->url_format.$fichier_map->nom_serveur;
				$place_location->setVariable('map_photo',addslashes($url_image_map));
				$place_location->setVariable('map_width',$width.'px');
				$place_location->setVariable('map_height',$height.'px');
				$place_location->showBlock('map_image');
			}else{
				$place_location->setVariable('map_photo', '');
				$place_location->hideBlock('map_image');
			}
		}*/

		/*if($location->name_place){
			$place_location->showBlock('place_name');
		}*/
		/*if($location->address){
			$place_location->showBlock('address');
		}*/
		/*if($location->name_city){
			$place_location->showBlock('city');
		}*/
		/*if($location->name_province){
			$place_location->showBlock('province');
		}
		if($location->name_district){
			$place_location->showBlock('district');
		}
		if($location->zip_code){
			$place_location->showBlock('zip_code');
		}*/

		$place_location->setVariable('latitude', $location->latitude);
		$place_location->setVariable('longitude', $location->longitude);

		//$place_location->showBlock('marker');
		
	}
	if(isset($_SESSION['id_property'])){
		$place_location->hideBlock('modify');
		$place_location->showBlock('create');
	}else{
		$place_location->showBlock('modify');
		$place_location->hideBlock('create');
	}
}else{
	$place_location->showBlock('create');
	$place_location->hideBlock('marker');
}

$place_location->stop();
$page->setBody($place_location);
$page->display();

?>